package com.hq.talk.view.frg;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hq.lib_comview.common.code.IntentKey;
import com.hq.lib_comview.common.utils.ImageUtils;
import com.hq.lib_comview.common.utils.StringUtils;
import com.hq.lib_comview.view.baseadp.CommonAdapter;
import com.hq.lib_comview.view.baseadp.ViewHolder;
import com.hq.lib_comview.view.selview.MyListView;
import com.hq.talk.R;
import com.hq.talk.common.BaseEvent;
import com.hq.talk.common.Session;
import com.hq.talk.model.CommItemModel;
import com.hq.talk.view.base.BaseCommFrg;
import com.hq.talk.view.ui.MySpaceAty;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 描述 ：ic_hom_mine
 * 作者 ：hq
 * 时间 ：2017/8/8 14:20
 */
public class MineFrg extends BaseCommFrg implements AdapterView.OnItemClickListener {

    //头像
    ImageView mImgUserHead;
    //昵称
    TextView mTxtUserName;

    @BindView(R.id.id_mine_listv)
    MyListView myListView;
    CommonAdapter<CommItemModel> mMineAdp;
    private List<CommItemModel> mItems = new ArrayList<CommItemModel>();

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.frg_mine, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        initHeadView();
        CommItemModel model = new CommItemModel(R.drawable.ic_my_company, getString(R.string.my_company), "", false, false);
        CommItemModel model1 = new CommItemModel(R.drawable.ic_my_order, getString(R.string.my_order), "", true, false);
        CommItemModel model2 = new CommItemModel(R.drawable.ic_my_account, getString(R.string.my_account), getString(R.string.no_login), true, false);
        CommItemModel model3 = new CommItemModel(R.drawable.ic_my_about, getString(R.string.my_about), "", true, false);
        mItems.add(model);
        mItems.add(model1);
        mItems.add(model2);
        mItems.add(model3);
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        mMineAdp = new CommonAdapter<CommItemModel>(frg, mItems, R.layout.item_mine) {
            @Override
            public void convert(ViewHolder helper, CommItemModel item) {
                if (null != item) {
                    helper.setBackgroundResource(R.id.id_mine_icon_img, item.getDrawable());
                    helper.setText(R.id.id_mine_title_txt, item.getName());
                    if (StringUtils.isEmpty(item.getmDesc())) {
                        helper.setText(R.id.id_mine_desc_txt, "");
                    } else {
                        helper.setText(R.id.id_mine_desc_txt, item.getmDesc());
                    }
                    if (item.isDivider()) {
                        helper.getView(R.id.id_line_1).setVisibility(View.GONE);
                        helper.getView(R.id.id_line_2).setVisibility(View.VISIBLE);
                    } else {
                        helper.getView(R.id.id_line_1).setVisibility(View.VISIBLE);
                        helper.getView(R.id.id_line_2).setVisibility(View.GONE);
                    }
                    if (item.isUpdate()) {
                        helper.getView(R.id.id_mine_remind_txt).setVisibility(View.VISIBLE);
                    } else {
                        helper.getView(R.id.id_mine_remind_txt).setVisibility(View.GONE);
                    }
                }
            }
        };
        myListView.setAdapter(mMineAdp);
        myListView.setOnItemClickListener(this);
    }

    /**
     * 初始化头布局
     */
    private void initHeadView() {
        View mHeadView = LayoutInflater.from(frg).inflate(R.layout.layout_mine_info, null);
        mImgUserHead = (ImageView) mHeadView.findViewById(R.id.id_user_head_img);
        mTxtUserName = (TextView) mHeadView.findViewById(R.id.id_user_name_txt);
        myListView.addHeaderView(mHeadView);
        setUserInfo();
        mHeadView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLogin(false)) {
                    showActivity(frg, MySpaceAty.class);
                }
            }
        });
    }

    /**
     * 设置用户信息
     */
    public void setUserInfo() {
        ImageUtils.showCircleImg(frg, Session.getUserHead(), R.drawable.ic_default_user_head, R.drawable.ic_default_user_head, mImgUserHead);
        if (StringUtils.isEmpty(Session.getUserName())) {
            mTxtUserName.setText("去登录");
        } else {
            mTxtUserName.setText(Session.getUserName());
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 1:
                //进入我的公司
                if (isLogin(true)) {

                }
                break;
            case 2:
                //进入我的订单
                if (isLogin(true)) {

                }
                break;
            case 3:
                //账号管理
                if (isLogin(true)) {

                }
                break;
            case 4:
                //关于
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        String data = event.data;
        if (TextUtils.equals(data, IntentKey.Login.KEY_LOGIN_SUCCESS)) {
            //登录成功
            setUserInfo();
            CommItemModel model = mItems.get(3);
            model.setmDesc(Session.getUserName());
            mMineAdp.notifyDataSetChanged();
        } else if (TextUtils.equals(data, IntentKey.Login.KEY_APP_EXIT)) {
            //退出登录
            setUserInfo();
            CommItemModel model = mItems.get(3);
            model.setmDesc(getString(R.string.no_login));
            mMineAdp.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
